<?php

class AuthController extends Zend_Controller_Action
{
    public function init()
    {
        $layout = Zend_Layout::getMvcInstance();
        $layout->disableLayout();
        
    }
    public function indexAction ()
    {
        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_RPS')); 
        if($auth->hasIdentity())
        {
            $this->_helper->redirector('index','index');
        } 
        $formLogin = new Default_Form_Auth();
        if($this->_request->getParam('type') == 'login_other_computer'){
            $this->view->mess = 'Bạn đã login trên máy khác!';
            $this->view->flag = 'error';
        }
        if($this->_request->isPost())
        {
            $form_data = $this->_request->getPost();
            if($formLogin->isValid($form_data))
            {   
                $adapter = new Zend_Auth_Adapter_DbTable(NULL,'user','username','password','MD5(?)');
                $adapter->setIdentity($formLogin->getValue('username'));
                $adapter->setCredential($formLogin->getValue('password'));
                $result = $auth->authenticate($adapter);
                if($result->isValid())
                {
					$modelUser = new Default_Model_User();
                    $user_check = $modelUser->getDataByUsername($formLogin->getValue('username'));
                    
                    if($user_check->status == 1){
                        $auth->getStorage()->write(array('username' => $user_check['username'], 'userId' => $user_check['userId']));
                        $modelUser->updateSessionId($user_check['userId'],session_id());
                        
                        $modelRole                      = new Default_Model_Role();
                        $roleCheck                      = $modelRole->getDataByUserId($user_check['userId']);
                        $gameList                       = array();
                        foreach($roleCheck as $row){
                            if(!$row['gameId']){
                                continue;
                            }
                            $gameId = json_decode($row['gameId']);
                            if(is_array($gameId)){
                                $gameList = array_merge($gameList,$gameId);
                                $gameList = array_unique($gameList);
                            }
                            if($row['homepage']){
                                $homepage = $row['homepage'];
                            }
                        }
                        if(count($gameList) == 1){
                            if(!isset($homepage)){
                                $homepage = '/default/transaction/index';
                            }
                            $this->_redirect($homepage.'/game/'.$gameList[0]);
                        }
                        
                        $redirectUrl        = $this->getRequest()->getParam('redirectUrl', '');
                        if($redirectUrl != ''){
                            $this->_redirect($redirectUrl);
                        }else{
                            $this->_helper->redirector('index','index');
                        }
                    }else{
                        $auth->clearIdentity();
                        $this->view->mess = 'Tài khoản của bạn đang tạm khóa!';
                        $this->view->flag = 'alert';
                        $this->view->form = $formLogin;
                    }
                }else{
                    $this->view->mess = 'Tên truy cập hoặc mật khẩu không đúng!';
                    $this->view->flag = 'error';
                    $this->view->form = $formLogin;
                }
            }else{
                $this->view->mess = 'Vui lòng kiểm tra lại thông tin đăng nhập!';
                $this->view->flag = 'error';
                $this->view->form = $formLogin;
            }
        }else{
            $this->view->form = $formLogin;
        }
    }
    public function logoutAction(){
        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_RPS'));
        $auth->clearIdentity();
        return $this->_redirect('/auth');
    }
}



